package com.whw.fy.service.impl;

import com.whw.fy.entity.InstructionalSupervision;
import com.whw.fy.entity.Message;
import com.whw.fy.mapper.*;
import com.whw.fy.utils.ListUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.*;

import com.whw.fy.entity.TeachingDiscussion;
import com.whw.fy.service.TeachingDiscussionService;
@Service
public class TeachingDiscussionServiceImpl implements TeachingDiscussionService{

    @Resource
    private TeachingDiscussionMapper teachingDiscussionMapper;
    @Resource
    private StaffMapper staffMapper;

    @Override
    public Message deleteByPrimaryKey(Integer teachingDiscussionId) {
        return teachingDiscussionMapper.deleteByPrimaryKey(teachingDiscussionId)>0?Message.success(null):Message.fail(null);
    }

    @Override
    public Message insertSelective(TeachingDiscussion record) {
        return teachingDiscussionMapper.insertSelective(record)>0?Message.success(null):Message.fail(null);
    }

    @Override
    public Message selectByPrimaryKey(Integer teachingDiscussionId) {
        return Message.success(null).add(initTeachingDiscussion(teachingDiscussionMapper.selectByPrimaryKey(teachingDiscussionId)));
    }

    @Override
    public Message updateByPrimaryKeySelective(TeachingDiscussion record) {
        return teachingDiscussionMapper.updateByPrimaryKeySelective(record)>0?Message.success(null):Message.fail(null);
    }

    @Override
    public Message updateTeachingDiscussionStatusByTeachingDiscussionId(Integer updatedTeachingDiscussionStatus,Integer teachingDiscussionId) {
        return teachingDiscussionMapper.updateTeachingDiscussionStatusByTeachingDiscussionId(updatedTeachingDiscussionStatus,teachingDiscussionId)>0?Message.success(null):Message.fail(null);
    }

    @Override
    public Message selectAll() {
        List<TeachingDiscussion> list = teachingDiscussionMapper.selectAll();
        for (TeachingDiscussion teachingDiscussion:list)
            initTeachingDiscussion(teachingDiscussion);
        return Message.success(null).add(list);

    }

    @Override
    public Message selectallByTeacherIdJurisdictionEight(int staffId) {
        List<TeachingDiscussion> allList = new ArrayList<>();
        List<TeachingDiscussion> list = teachingDiscussionMapper.selectAllByTeachingDiscussionTeacherId(staffId);
        List<TeachingDiscussion> list1 = teachingDiscussionMapper.selectByTeachingDiscussionParticipantsMohu(String.valueOf(staffId));
        for (TeachingDiscussion teachingDiscussion:list)
            if (teachingDiscussion != null)
                allList.add(initTeachingDiscussion(teachingDiscussion));
        for (TeachingDiscussion teachingDiscussion:list1)
            if (teachingDiscussion != null)
                allList.add(initTeachingDiscussion(teachingDiscussion));
        Set<TeachingDiscussion> s = new HashSet<TeachingDiscussion>();
        s.addAll(allList);
        return Message.success(null).add(s);
    }

    @Override
    public Message selectallByMedicalDirectorIdJurisdictionZero(int staffId) {
        List<TeachingDiscussion> list = teachingDiscussionMapper.selectAll();
        for (TeachingDiscussion teachingDiscussion:list)
            initTeachingDiscussion(teachingDiscussion);
        return Message.success(null).add(list);
    }

    @Override
    public Message selectallByTeachingDirectorJurisdictionSix(int staffId) {
        List<TeachingDiscussion> allList = new ArrayList<>();
        List<TeachingDiscussion> list = teachingDiscussionMapper.selectByTeachingDiscussionTeachingSupervisorId(staffId);
        List<TeachingDiscussion> list1 = teachingDiscussionMapper.selectByTeachingDiscussionParticipantsMohu(String.valueOf(staffId));
        for (TeachingDiscussion teachingDiscussion:list)
            if (teachingDiscussion != null)
                allList.add(initTeachingDiscussion(teachingDiscussion));
        for (TeachingDiscussion teachingDiscussion:list1)
            if (teachingDiscussion != null)
                allList.add(initTeachingDiscussion(teachingDiscussion));
        Set<TeachingDiscussion> s = new HashSet<TeachingDiscussion>();
        s.addAll(allList);
        return Message.success(null).add(s);
    }

    @Override
    public Message selectallByprincipalTeacherJurisdictionSeven(int staffId) {
        List<TeachingDiscussion> allList = new ArrayList<>();
        List<TeachingDiscussion> list = teachingDiscussionMapper.selectByTeachingDiscussionPrincipalTeacherId(staffId);
        List<TeachingDiscussion> list1 = teachingDiscussionMapper.selectByTeachingDiscussionParticipantsMohu(String.valueOf(staffId));
        for (TeachingDiscussion teachingDiscussion:list)
            if (teachingDiscussion != null)
                allList.add(initTeachingDiscussion(teachingDiscussion));
        for (TeachingDiscussion teachingDiscussion:list1)
            if (teachingDiscussion != null)
                allList.add(initTeachingDiscussion(teachingDiscussion));
        Set<TeachingDiscussion> s = new HashSet<TeachingDiscussion>();
        s.addAll(allList);
        return Message.success(null).add(s);
    }

    private TeachingDiscussion initTeachingDiscussion(TeachingDiscussion record){
        //设置发起人名称
        if (record.getTeachingDiscussionPostUserId() != null) {
            record.setTeachingDiscussionPostUserName(staffMapper.selectByPrimaryKey(record.getTeachingDiscussionPostUserId()).getStaffName());
        }
        //设置授课老师名称
        if (record.getTeachingDiscussionTeacherId()!=null)
            record.setTeachingDiscussionTeacherName(staffMapper.selectByPrimaryKey(record.getTeachingDiscussionTeacherId()).getStaffName());
        //设置教学主管名称
        if (record.getTeachingDiscussionTeachingSupervisorId()!=null)
            record.setTeachingDiscussionTeachingSupervisorName(staffMapper.selectByPrimaryKey(record.getTeachingDiscussionTeachingSupervisorId()).getStaffName());
        //设置主责老师名称
        if (record.getTeachingDiscussionPrincipalTeacherId()!=null)
            record.setTeachingDiscussionPrincipalTeacherName(staffMapper.selectByPrimaryKey(record.getTeachingDiscussionPrincipalTeacherId()).getStaffName());

        return record;
    }
}
